<?php
/**
 * videocontroller.php
 * Create on 2016-09-12 10:40:35
 * Create by yuebin
 */
namespace haibao\cms\business\control\pc\channel\video;

use haibao\frame\data\query\Condition;
use haibao\cms\model\filter\Video as VideoFilter;
use haibao\cms\model\filter\ImageLibrary as ImageLibraryFilter;
use \haibao\cms\model\filter\VideoProperty as VideoPropertyFilter;
class VideoController extends \haibao\cms\business\control\VideoController{
	
	private $videoData;
	private $arrSource = array(
			'Star'=>2,
			'manner'=>230,
	);
	public function __construct($updateTime = null,$controlId,$isPublish){
		
		parent::__construct($updateTime,$controlId,$isPublish);
	}
	
	public function displayTemplateHtml(){
		$controlData = $this->getParameter(self::$CONTROL_DATA);
		$content = $this->getParameter(self::$CONTROL_CONTENT);
		if($controlData['manual']){
			$this->getVideoData($controlData['manual'],$controlData['auto']);
		}
		$newList = $this->getNewVideoList(1,true,3,false,false);
		$this->assign('newList', $newList[0]);
		$path = $this->generateHtmlPath(__CLASS__);
		$this->generateHtml($path,$content);
		$content = $this->getHtml($path);
		$content = $this->replaceCDNLink($content);
		return $content;
	}
	
	public function getVideoData($dataCondition,$autoData){
		$isFeature = false;
		$result = array();
		if($dataCondition['source'] == 'Foucus'){
			$this->getFocusVideo($autoData);
		}elseif($dataCondition['source'] == 'Hot'){
			$isFeature = true;
			$result = $this->getHotVideo($dataCondition,$autoData);
		}elseif($dataCondition['source'] == 'Cate'){
			$isFeature = true;
			$cId = $dataCondition['Category'];
			$arrCategory = self::getVideoCategory();
			$arrCateId = $first = $arrCate = array();
			if(isset($arrCategory[$cId])){
				$arrCateId = $arrCategory[$cId];
			}
			$result = $this->searchVideo($dataCondition,array(),$arrCateId);
			if($cId == 2){
				$first = array_shift($result);
			}elseif($cId == 230 || $cId == 231 || $cId == 234){
				$tmpArr = array_chunk($result,2);
				$first = array_shift($tmpArr);
				$result = array_shift($tmpArr);
			}
			$i = 0;
			foreach ($arrCateId as $key=>$list){
				$arrCate[$i]['Cate'] = $key;
				$arrCate[$i]['Cid'] = $list;
				$i++;
			}
			$this->assign('first', $first);
			$this->assign('category', $arrCate);
		}
		/* $featureUrls = $featureIds = array();
		if($result){
			foreach ($result as $list){
				if($list->FeatureId) array_push($featureIds, $list->FeatureId);
			}
			$featureUrls = self::getFeatureUrlByFeatureIds(array_unique($featureIds));
			foreach ($result as $key=>$list){
				if(isset($featureUrls[$list->FeatureId])){
					$result[$key]->FeatureId = $featureUrls[$list->FeatureId];
				}
			}
		} */
		$this->assign('result', $result);
		$this->assign('videoLists', $result);
		$videoHost = \haibao\classlibrary\Config::getConfig(\haibao\classlibrary\Config::CLASSLIBRARY_CONFIG_HOST_VIDEO);
		$userHost = \haibao\classlibrary\Config::getConfig(\haibao\classlibrary\Config::CLASSLIBRARY_CONFIG_HOST_USER);
		$this->assign('userHost', $userHost);
		$this->assign('videoHost', $videoHost);
	}
	
	private function getHotVideo($dataCondition,$autoData){
		$multiDataCount = count($autoData);
		$videoIds = $arrOrder = array();
		if($multiDataCount){
			foreach ($autoData as $value){
				$multiData = json_decode($value['ParameterValue'],true);
				$tmp = explode(',', $multiData['multiData']);
				$order = $value['OrderNo'];
				$videoIds[] = $tmp[0];
				$arrOrder[$tmp[0]] = $order;
			}
			asort($arrOrder);
			$arrSortVid = array_keys($arrOrder);
			$arrOrder = array_flip($arrSortVid);
		}
		if(count($videoIds) < 6){
			if($videoIds){
				$autoData = $this->getVideoByIds($videoIds);
			}
			$videoList = $this->searchVideo($dataCondition,$videoIds);
			$result = array_merge($videoList,$autoData);
		}else{
			$videoList = $this->getVideoByIds($videoIds);
			$result = $resArr = array();
			foreach ($videoList as $list){
				if(isset($arrOrder[$list->Id])){
					$resArr[$arrOrder[$list->Id]] = $list;
				}
			}
			ksort($resArr);
			$i=0;
			foreach($resArr as $val){
				$result[$i] = $val;
				$i++;
			}
		}
		return $result;
	}
	
	private function searchVideo($dataCondition,$videoIds=array(),$categoryId=array()){
		$videoFilter = new VideoFilter();
		$videoFileFilter = new \haibao\cms\model\filter\VideoFile();
		$videoCategoryFilter = new \haibao\cms\model\filter\VideoCategory();
		$videoPropertyFilter = new VideoPropertyFilter();
		$videoFilter->select(array(
				VideoFilter::CONDITION_FIELD_NAME_ID,
				VideoFilter::CONDITION_FIELD_NAME_TITLE,
				VideoFilter::CONDITION_FIELD_NAME_PROPICURL,
				VideoFilter::CONDOTION_FIELD_NAME_DESCRIPTION,
				VideoFilter::CONDITION_FIELD_NAME_PUBLISHDATE,
				VideoFilter::CONDITION_FIELD_NAME_PUBLISHED
		));
		$videoCategoryFilter->select(array(
    	    \haibao\cms\model\filter\VideoCategory::CONDITION_FIELD_NAME_ID,
    	    \haibao\cms\model\filter\VideoCategory::CONDITION_FIELD_NAME_CATEGORYNAME
    	));
    	$videoFilter->leftJoin($videoCategoryFilter,null,VideoFilter::CONDITION_FIELD_NAME_CATEGORYID,\haibao\cms\model\filter\VideoCategory::CONDITION_FIELD_NAME_ID,VideoFilter::CONDITION_FIELD_NAME_CATEGORY,false);
    	$videoPropertyFilter->select(array(
    			VideoPropertyFilter::CONDITION_FIELD_NAME_COMMENTNUM,
    			VideoPropertyFilter::CONDITION_FIELD_NAME_ID,
    			VideoPropertyFilter::CONDITION_FIELD_NAME_PLAYNUM,
    			VideoPropertyFilter::CONDITION_FIELD_NAME_PLAYNUMSHOW,
    			VideoPropertyFilter::CONDITION_FIELD_NAME_VIDEOID
    	));
    	$videoFilter->leftJoin($videoPropertyFilter,null,VideoFilter::CONDITION_FIELD_NAME_ID,VideoPropertyFilter::CONDITION_FIELD_NAME_VIDEOID,VideoFilter::CONDITION_FIELD_NAME_PROPERTY,false);
    	$imageLibraryFilter = new ImageLibraryFilter();
    	$imageLibraryFilter->select(array(
    			ImageLibraryFilter::CONDITION_FIELD_NAME_FILENAME,
    	));
    	$videoFilter->innerJoin($imageLibraryFilter,$videoFilter,VideoFilter::CONDITION_FIELD_NAME_PICID,ImageLibraryFilter::CONDITION_FIELD_NAME_ID,VideoFilter::CONDITION_FIELD_NAME_PIC_URL,false);
    	$videoFilter->where(VideoFilter::CONDITION_FIELD_NAME_STATUS, Condition::CONDITION_EQUAL,\haibao\cms\model\data\Video::STATUS_ENABLE);
    	$videoFilter->where(VideoFilter::CONDITION_FIELD_NAME_PUBLISHED, Condition::CONDITION_EQUAL,\haibao\cms\model\data\Video::PUBLISHED_OK);
    	if($categoryId){
    		$videoFilter->where(VideoFilter::CONDITION_FIELD_NAME_CATEGORYID,Condition::CONDITION_IN,$categoryId);
    		$videoFilter->order(VideoFilter::CONDITION_FIELD_NAME_PUBLISHDATE,false);
    	}
    	if($dataCondition['source'] == 'Hot'){
    		$startDate = date('Y-m-d',strtotime('-1 months'));
    		$videoFilter->where(VideoFilter::CONDITION_FIELD_NAME_PUBLISHDATE, Condition::CONDITION_GREATEREQUAL, $startDate);
    		$videoPropertyFilter->order(VideoPropertyFilter::CONDITION_FIELD_NAME_PLAYNUMSHOW,false);
    	}
    	$multiDataCount = count($videoIds);
    	if($multiDataCount){
    		$videoFilter->where(VideoFilter::CONDITION_FIELD_NAME_ID, Condition::CONDITION_NOTIN, $videoIds);
    	}
    	$limit = $dataCondition['limit'] - $multiDataCount;
    	$videoFilter->limit($limit);
    	$data = new \haibao\cms\data\Video();
    	$result = $data->getAll($videoFilter);
    	foreach ($result as $key=>$list){
    		$result[$key]->PublishDate = $list->PublishDate->format('Y.m.d');
    	}
    	return $result;
	}
	
	private function getVideoByIds($videoIds){
		$videoFilter = new VideoFilter();
		$videoFileFilter = new \haibao\cms\model\filter\VideoFile();
		$videoCategoryFilter = new \haibao\cms\model\filter\VideoCategory();
		$videoPropertyFilter = new VideoPropertyFilter();
		$videoFilter->select(array(
				VideoFilter::CONDITION_FIELD_NAME_ID,
				VideoFilter::CONDITION_FIELD_NAME_TITLE,
				VideoFilter::CONDITION_FIELD_NAME_PUBLISHDATE,
				VideoFilter::CONDITION_FIELD_NAME_PUBLISHED
		));
		$videoCategoryFilter->select(array(
				\haibao\cms\model\filter\VideoCategory::CONDITION_FIELD_NAME_ID,
				\haibao\cms\model\filter\VideoCategory::CONDITION_FIELD_NAME_CATEGORYNAME
		));
		$videoFilter->leftJoin($videoCategoryFilter,null,VideoFilter::CONDITION_FIELD_NAME_CATEGORYID,\haibao\cms\model\filter\VideoCategory::CONDITION_FIELD_NAME_ID,VideoFilter::CONDITION_FIELD_NAME_CATEGORY,false);
		$videoPropertyFilter->select(array(
				VideoPropertyFilter::CONDITION_FIELD_NAME_ID,
				VideoPropertyFilter::CONDITION_FIELD_NAME_PLAYNUMSHOW,
				VideoPropertyFilter::CONDITION_FIELD_NAME_VIDEOID
		));
		$videoFilter->leftJoin($videoPropertyFilter,null,VideoFilter::CONDITION_FIELD_NAME_ID,VideoPropertyFilter::CONDITION_FIELD_NAME_VIDEOID,VideoFilter::CONDITION_FIELD_NAME_PROPERTY,false);
		$imageLibraryFilter = new ImageLibraryFilter();
		$imageLibraryFilter->select(array(
				ImageLibraryFilter::CONDITION_FIELD_NAME_FILENAME,
		));
		$videoFilter->innerJoin($imageLibraryFilter,$videoFilter,VideoFilter::CONDITION_FIELD_NAME_PICID,ImageLibraryFilter::CONDITION_FIELD_NAME_ID,VideoFilter::CONDITION_FIELD_NAME_PIC_URL,false);
		$videoFilter->where(VideoFilter::CONDITION_FIELD_NAME_STATUS, Condition::CONDITION_EQUAL,\haibao\cms\model\data\Video::STATUS_ENABLE);
		$videoFilter->where(VideoFilter::CONDITION_FIELD_NAME_PUBLISHED, Condition::CONDITION_EQUAL,\haibao\cms\model\data\Video::PUBLISHED_OK);
		$videoFilter->where(VideoFilter::CONDITION_FIELD_NAME_ID, Condition::CONDITION_IN, $videoIds);
		$data = new \haibao\cms\data\Video();
		$result = $data->getAll($videoFilter);
		return $result;
	}
	
	private function getFocusVideo($autoData){
		$multiDataCount = count($autoData);
		$videoIds = $arrOrder = array();
		if($multiDataCount){
			foreach ($autoData as $value){
				$multiData = json_decode($value['ParameterValue'],true);
				$tmp = explode(',', $multiData['multiData']);
				$order = $value['OrderNo'];
				$videoIds[] = $tmp[0];
				$arrOrder[$tmp[0]] = $order;
			}
			asort($arrOrder);
			$arrSortVid = array_keys($arrOrder);
			$arrOrder = array_flip($arrSortVid);
		}
		$videoFilter = new VideoFilter();
		$videoFileFilter = new \haibao\cms\model\filter\VideoFile();
		$videoCategoryFilter = new \haibao\cms\model\filter\VideoCategory();
		 
		$videoFilter->select(array(
				VideoFilter::CONDITION_FIELD_NAME_ID,
				VideoFilter::CONDITION_FIELD_NAME_TITLE,
				VideoFilter::CONDITION_FIELD_NAME_PUBLISHDATE,
				VideoFilter::CONDITION_FIELD_NAME_FOCUSPIC,
				VideoFilter::CONDITION_FIELD_NAME_FOCUSVIDEO,
				VideoFilter::CONDITION_FIELD_NAME_PUBLISHED
		));
		if($videoIds){
			$videoFilter->where(VideoFilter::CONDITION_FIELD_NAME_ID, Condition::CONDITION_IN, $videoIds);
		}else{
			$videoFilter->where(VideoFilter::CONDITION_FIELD_NAME_STATUS, Condition::CONDITION_EQUAL,\haibao\cms\model\data\Video::STATUS_ENABLE);
			$videoFilter->where(VideoFilter::CONDITION_FIELD_NAME_PUBLISHED, Condition::CONDITION_EQUAL,\haibao\cms\model\data\Video::PUBLISHED_OK);
			$videoFilter->where(VideoFilter::CONDITION_FIELD_NAME_FOCUSPIC_STATE, Condition::CONDITION_EQUAL,\haibao\cms\model\data\Video::FOCUSPICSTATE_OK);
		}
		
		$data = new \haibao\cms\data\Video();
		$videoInfo = $data->getAll($videoFilter);
		$result = array();
		foreach ($videoInfo as $list){
			$list->FocusPic = (!empty($list->FocusPic))?self::getImageLink($list->FocusPic,0,0):'';
			$list->Title = mb_substr($list->Title,0,20,'utf-8');
			//$path = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\Config::CLASSLIBRARY_CONFIG_CDN_DOMAIN).$list->FocusVideo;
			$path = 'http://c1.haibao.cn'.$list->FocusVideo;
			$list->FocusVideo = $path;
			if($videoIds){
				if(isset($arrOrder[$list->Id])){
					$result[$arrOrder[$list->Id]] = $list;
				}
			}
		}
		
		if($videoIds){
			ksort($result);
			$this->assign('videoModel', $result);
		}else{
			$this->assign('videoModel', $videoInfo);
		}
	}
}